﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SHW_UP.BP;

namespace SHW_UP.UI
{
    public partial class frmGPRS : Form
    {
        public frmGPRS()
        {
            InitializeComponent();
        }

        private void bindTcpUdp()
        {
            DataTable dttcp = new DataTable();
            dttcp.Columns.Add("Value");
            dttcp.Columns.Add("Name");
            DataRow drSex;
            drSex = dttcp.NewRow();
            drSex[0] = "0";
            drSex[1] = "TCP";
            dttcp.Rows.Add(drSex);
            drSex = dttcp.NewRow();
            drSex[0] = "1";
            drSex[1] = "UDP";
            dttcp.Rows.Add(drSex);
            this.tcpCombox.ValueMember = "Value";
            this.tcpCombox.DisplayMember = "Name";
            this.tcpCombox.DataSource = dttcp;
            this.tcpCombox.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private DataTable dt = new DataTable();
        private void frmGPRS_Load(object sender, EventArgs e)
        {
            bindTcpUdp();
            this.gprsGridView.Columns.Clear();
            this.gprsGridView.ReadOnly = true;
            dt.Columns.Add("APN");
            dt.Columns.Add("通信类型");
            dt.Columns.Add("IP地址");
            dt.Columns.Add("端口号");
            dt.Columns.Add("终端号码");
            dt.Columns.Add("APN用户名");
            dt.Columns.Add("APN密码");
            DataRow dr1 = dt.NewRow();
            dr1[0] = InfoUtils.parTable["apn"]; ;
            dr1[1] = "TCP";
            dr1[2] = InfoUtils.parTable["ip"];
            dr1[3] = InfoUtils.parTable["port"];
            dr1[4] = InfoUtils.parTable["tid"];
            dr1[5] = InfoUtils.parTable["apnusername"];
            dr1[6] = InfoUtils.parTable["apnpasswd"];
            dt.Rows.Add(dr1);
            this.gprsGridView.DataSource = dt;
        }

        private void gprsModify_Click(object sender, EventArgs e)
        {
            string passwd = string.Empty;
            if (InfoUtils.parTable.Contains("passwd"))
            {
                passwd = (string)InfoUtils.parTable["passwd"];
            }
            else
            {
                passwd = "000000";
            }
            string str = "GPRS," + passwd + "," + this.apnText.Text + "," + this.tcpCombox.SelectedValue + "," + this.ipText1.Text + "." + this.ipText2.Text + "." + this.ipText3.Text + "." + this.ipText4.Text + "," + this.portText.Text + "," + this.terminalText.Text+","+"0,";
            if (!this.apnText.Text.ToLower().Equals("cmnet") && !this.apnText.Text.ToLower().Equals("uninet"))
            {
                if (this.apnUsernameText.Text.Length == 0 || this.apnPasswdText.Text.Length == 0)
                {
                    MessageBox.Show("非公网，请输入用户名及密码");
                    return;
                }
                else
                {
                    str = str + this.apnUsernameText.Text + "," + this.apnPasswdText.Text + ",";
                }
            }
            if (InfoUtils.sendInfoTable.ContainsKey("GPRS"))
            {
                InfoUtils.sendInfoTable.Remove("GPRS");
            }
            InfoUtils.sendInfoTable.Add("GPRS", str);
            this.gprsGridView.Rows[0].Cells[0].Value=this.apnText.Text;
            this.gprsGridView.Rows[0].Cells[1].Value=this.tcpCombox.SelectedText;
            this.gprsGridView.Rows[0].Cells[2].Value=this.ipText1.Text + "." + this.ipText2.Text + "." + this.ipText3.Text + "." + this.ipText4.Text;
            this.gprsGridView.Rows[0].Cells[3].Value=this.portText.Text;
            this.gprsGridView.Rows[0].Cells[4].Value=this.terminalText.Text;
            this.gprsGridView.Rows[0].Cells[5].Value=this.apnUsernameText.Text;
            this.gprsGridView.Rows[0].Cells[6].Value=this.apnPasswdText.Text;
            //SerialPortSender.sendStrInfo(str, this.serialPort);
        }

        private void ipText1_TextKeyUp(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(this.ipText1.Text) > 255)
                {
                    this.ipText1.Text = "255";
                }
            }
            catch (Exception es)
            {
                this.ipText1.Text = "";
                Console.WriteLine(es.Message);
            }
        }


        private void portText_TextKeyUp(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(this.portText.Text) > 65535)
                {
                    this.portText.Text = "65535";
                }
            }
            catch (Exception es)
            {
                this.portText.Text = "";
                Console.WriteLine(es.Message);
            }
        }

        private void ipText2_TextKeyUp(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(this.ipText2.Text) > 255)
                {
                    this.ipText2.Text = "255";
                }
            }
            catch (Exception es)
            {
                this.ipText2.Text = "";
                Console.WriteLine(es.Message);
            }
        }

        private void ipText3_TextKeyUp(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(this.ipText3.Text) > 255)
                {
                    this.ipText3.Text = "255";
                }
            }
            catch (Exception es)
            {
                this.ipText3.Text = "";
                Console.WriteLine(es.Message);
            }
        }

        private void ipText4_TextKeyUp(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(this.ipText4.Text) > 255)
                {
                    this.ipText4.Text = "255";
                }
            }
            catch (Exception es)
            {
                this.ipText4.Text = "";
                Console.WriteLine(es.Message);
            }
        }

        private void GridView_RowClick(object sender, EventArgs e)
        {
            if (this.gprsGridView.SelectedRows.Count > 0)
            {
                this.apnText.Text = this.gprsGridView.SelectedRows[0].Cells[0].Value.ToString();
                this.tcpCombox.SelectedText = this.gprsGridView.SelectedRows[0].Cells[1].Value.ToString();
                string ipstr = this.gprsGridView.SelectedRows[0].Cells[2].Value.ToString();
                string ip1 = ipstr.Substring(0, ipstr.IndexOf("."));
                ipstr = ipstr.Substring(ipstr.IndexOf(".")+1);
                string ip2 = ipstr.Substring(0, ipstr.IndexOf("."));
                ipstr = ipstr.Substring(ipstr.IndexOf(".") + 1);
                string ip3 = ipstr.Substring(0, ipstr.IndexOf("."));
                ipstr = ipstr.Substring(ipstr.IndexOf(".") + 1);
                string ip4 = ipstr.Substring(0, ipstr.Length);
                this.ipText1.Text = ip1;
                this.ipText2.Text = ip2;
                this.ipText3.Text = ip3;
                this.ipText4.Text = ip4;
                this.portText.Text = this.gprsGridView.SelectedRows[0].Cells[3].Value.ToString();
                this.terminalText.Text = this.gprsGridView.SelectedRows[0].Cells[4].Value.ToString();
                this.apnUsernameText.Text = this.gprsGridView.SelectedRows[0].Cells[5].Value.ToString();
                this.apnPasswdText.Text = this.gprsGridView.SelectedRows[0].Cells[6].Value.ToString();
            }
        }

        private void gprsGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void gprsRefresh_Click(object sender, EventArgs e)
        {
            this.gprsGridView.Rows[0].Cells[0].Value = InfoUtils.parTable["apn"];
            this.gprsGridView.Rows[0].Cells[1].Value = "TCP";
            this.gprsGridView.Rows[0].Cells[2].Value = InfoUtils.parTable["ip"];
            this.gprsGridView.Rows[0].Cells[3].Value = InfoUtils.parTable["port"];
            this.gprsGridView.Rows[0].Cells[4].Value = InfoUtils.parTable["tid"];
            if (InfoUtils.parTable.ContainsKey("apnusername"))
            {
                this.gprsGridView.Rows[0].Cells[5].Value = InfoUtils.parTable["apnusername"];
                this.gprsGridView.Rows[0].Cells[6].Value = InfoUtils.parTable["apnpasswd"];
            }
            else
            {
                this.gprsGridView.Rows[0].Cells[5].Value = "";
                this.gprsGridView.Rows[0].Cells[6].Value = "";
            }
            this.gprsGridView.ClearSelection();
        }
    }
}
